package com.tjetc.service.impl;

import com.tjetc.dao.FeeDao;
import com.tjetc.dao.MedicineDao;
import com.tjetc.dao.OrderDao;
import com.tjetc.dao.OtcDao;
import com.tjetc.dao.impl.FeeDaoImpl;
import com.tjetc.dao.impl.MedicineDaoImpl;
import com.tjetc.dao.impl.OrderDaoImpl;
import com.tjetc.dao.impl.OtcDaoImpl;
import com.tjetc.domain.Fee;
import com.tjetc.domain.Medicine;
import com.tjetc.domain.Medreport;
import com.tjetc.domain.Otc;
import com.tjetc.service.FeeService;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;

import java.sql.SQLException;
import java.util.List;

public class FeeServiceImpl implements FeeService {
    private FeeDao feeDao=new FeeDaoImpl();
    private OtcDao otcDao = new OtcDaoImpl();
    private MedicineDao medicineDao = new MedicineDaoImpl();
    private OrderDao orderDao = new OrderDaoImpl();

    @Override
    public List<Fee> listNoDeal() {
        try {
            return feeDao.listNoDeal();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Medreport> listByDate(String date) {
        try {
            return feeDao.listByDate(date);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Fee> list(PageInfo<Fee> pageInfo) {
        try {
            return feeDao.list(pageInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount() {
        try {
            return feeDao.getCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean add(Fee fee,int oid) {
        try {
            Db.startTransaction();
            //添加到fee表
            feeDao.add(fee);
            //修改order表状态
            orderDao.updateStatusById(oid);
            Db.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        }finally {
            Db.close();
        }
        return false;
    }

    @Override
    public Fee listMyFee(int rid) {
        try {
            return feeDao.listMyFee(rid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean updateStatus(int id) {
        try {
            feeDao.updateStatus(id);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean finishFee(int id, int rid) {
        try {
            Db.startTransaction();
            //改变缴费的状态
            feeDao.finishFee(id);
            //根据rid查询要就减少的药品
            List<Otc> otcList = otcDao.listByRid(rid);
            for (Otc otc : otcList) {
                Medicine medicine = medicineDao.getById(otc.getMid());
                if (medicine.getRestamount() < otc.getNum()){
                    return false;
                }else{
                    medicineDao.cutDown(otc.getMid(),otc.getNum());
                }
            }
            Db.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        }finally {
            Db.close();
        }
        return false;
    }

    @Override
    public boolean rollBackStatus() {
        try {
            int maxId = feeDao.getMaxId();
            feeDao.rollBackStatus(maxId);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
